"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Compatibility
Case Name   : load语句任意指定一个参数
Create At   : 2023/06/02
Owner       : n@ningyali
Description :
        1.建表
        2.指定参数replace
        3.指定参数ignore
        4.指定导入编码格式是sql_ascii
        5.指定参数ignore number lines
        6.指定参数ignore number rows
        7.清理环境
Expect      :
        1.创建成功
        2.成功
        3.成功
        4.成功
        5.成功
        6.成功
        7.成功
History     :
"""
import os
import unittest

from yat.test import Node

from testcase.COMPATIBILITY.GRAMMAR.LOAD. \
    Opengauss_Compatibility_Grammar_Load_Common import LoadData
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class LoadData0036(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.load_data = LoadData()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.case_lable = 'load_data_0036'
        self.db_name = f'db_{self.case_lable}'
        self.t_name = f't_{self.case_lable}'
        self.file_path = os.path.join('/home', self.pri_user.ssh_user)
        self.cols = 'c_id int(11)auto_increment primary key,' \
                    'c_name varchar(50),c_age int(11),' \
                    'c_gender varchar(11),c_cid int(11)'

        text1 = '----pre_step1：创建数据文件；expect:创建成功----'
        self.log.info(text1)
        self.data = "2,'姓名1',2,'男',22\n" \
                    "3	'姓名02'	4	'女'	44\n" \
                    "4	'姓名2'	5	'女'	55\n" \
                    "5/'姓名3'/4/男/44\n" \
                    "6	姓名4 2	男	22"
        res = self.load_data.create_data_file(self.pri_user, self.case_lable,
                                              self.data)
        self.assertTrue(res, '执行失败:' + text1)

        text2 = '----pre_step2：创建兼容B库；expect:创建成功----'
        self.log.info(text2)
        res = self.load_data.create_b_db(self.pri_sh, self.db_name)
        self.assertTrue(res, '执行失败:' + text2)

    def test_load(self):
        text = '----step1：建表；expect：建表成功----'
        self.log.info(text)
        res = self.load_data.create_table(self.pri_sh, self.db_name,
                                          self.t_name, self.cols)
        self.assertTrue(res, '执行失败:' + text)

        text = '----step2：指定参数replace；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_1.txt')
        sql1 = f"set dolphin.sql_mode = ansi_quotes;" \
            f"load data infile '{f_path}' " \
            f"replace into table {self.t_name};" \
            f"select * from {self.t_name};"
        res1 = self.pri_sh.execut_db_sql(sql1, dbname=self.db_name)
        self.log.info(res1)
        rows, _ = self.common.parse_sql_query_res(res1)
        expect = [['2', '', '', '', ''], ['3', "'姓名02'", '4', "'女'", '44'],
                  ['4', "'姓名2'", '5', "'女'", '55'], ['5', '', '', '', ''],
                  ['6', '姓名4 2', '0', '22', '']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '----step3：指定参数ignore；expect：成功----'
        self.log.info(text)
        sql2 = f'truncate table {self.t_name};' + \
               sql1.replace('replace', 'ignore')
        res2 = self.pri_sh.execut_db_sql(sql2, dbname=self.db_name)
        self.log.info(res2)
        rows, _ = self.common.parse_sql_query_res(res1)
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '----step4：指定导入编码格式是sql_ascii；expect：成功----'
        self.log.info(text)
        sql3 = sql2.replace(
            f"into table {self.t_name}",
            f"into table {self.t_name} character set 'sql_ascii'")
        res3 = self.pri_sh.execut_db_sql(sql3, dbname=self.db_name)
        self.log.info(res3)
        rows, _ = self.common.parse_sql_query_res(res1)
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '----step5：指定参数ignore number lines；expect：成功----'
        self.log.info(text)
        sql4 = sql2.replace(
            f"into table {self.t_name}",
            f"into table {self.t_name} ignore 4 lines")
        res4 = self.pri_sh.execut_db_sql(sql4, dbname=self.db_name)
        self.log.info(res4)
        rows, _ = self.common.parse_sql_query_res(res4)
        self.assertEqual(rows, expect[4:], '执行失败' + text)

        text = '----step6：指定参数ignore number rows；expect：成功----'
        self.log.info(text)
        sql5 = sql2.replace(
            f"into table {self.t_name}",
            f"into table {self.t_name} ignore 2 rows")
        res5 = self.pri_sh.execut_db_sql(sql5, dbname=self.db_name)
        self.log.info(res5)
        rows, _ = self.common.parse_sql_query_res(res5)
        self.assertEqual(rows, expect[2:], '执行失败' + text)

    def tearDown(self):
        self.log.info('----step7：清理环境；expect：清理成功----')
        res = self.load_data.clear_env(self.pri_sh, self.pri_user,
                                       self.db_name,
                                       os.path.join(self.file_path,
                                                    f"{self.case_lable}*"))
        self.assertTrue(res, '执行失败:清理环境')
        self.log.info(f'----{os.path.basename(__file__)} end----')
